#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

void Print(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void Swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

void QuickSort(int* a, int begin, int end)
{
	if (begin >= end)
		return;
	int prev = begin;
	int cur = prev + 1;
	int keyi = begin;

	while (cur <= end)
	{
		if (a[cur] < a[keyi] && ++prev != cur)
		{
			Swap(&a[cur], &a[prev]);
		}
		cur++;
	}
	Swap(&a[keyi], &a[prev]);
	keyi = prev;
	//[begin,keyi-1] keyi [keyi+1,end]
	QuickSort(a, begin, keyi - 1);
	QuickSort(a, keyi+1, end);
}

void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap /= 2;
		for (int i = 0; i < n-gap; i += gap)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (a[end] > tmp)
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tmp;
		}

	}
}

int main()
{
	int arr[] = { 1,6,7,34,2,5,7,9,0,-4 };
	int sz = sizeof(arr) / sizeof(arr[0]);

	Print(arr, sz);
	//QuickSort(arr, 0, sz - 1);
	ShellSort(arr, sz);
	Print(arr, sz);
	return 0;
}